package com.ys.nms.module.business.service.processors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import com.ys.nms.module.business.dal.dataobject.devices.DevicesDO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.ys.nms.module.business.controller.admin.processors.vo.*;
import com.ys.nms.module.business.dal.dataobject.processors.ProcessorsDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.processors.ProcessorsMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 处理器 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ProcessorsServiceImpl implements ProcessorsService {

    @Resource
    private ProcessorsMapper processorsMapper;

    @Override
    public Integer createProcessors(ProcessorsSaveReqVO createReqVO) {
        // 插入
        ProcessorsDO processors = BeanUtils.toBean(createReqVO, ProcessorsDO.class);
        processorsMapper.insert(processors);

        // 返回
        return processors.getProcessorId();
    }

    @Override
    public void updateProcessors(ProcessorsSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessorsExists(updateReqVO.getProcessorId());
        // 更新
        ProcessorsDO updateObj = BeanUtils.toBean(updateReqVO, ProcessorsDO.class);
        processorsMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessors(Integer id) {
        // 校验存在
        validateProcessorsExists(id);
        // 删除
        processorsMapper.deleteById(id);
    }

    @Override
    public void deleteProcessorsListByIds(List<Integer> ids) {
        // 删除
        processorsMapper.deleteByIds(ids);
    }


    private void validateProcessorsExists(Integer id) {
        if (processorsMapper.selectById(id) == null) {
            throw exception(PROCESSORS_NOT_EXISTS);
        }
    }

    @Override
    public ProcessorsDO getProcessors(Integer id) {
        return processorsMapper.selectById(id);
    }

    @Override
    public PageResult<ProcessorsDO> getProcessorsPage(ProcessorsPageReqVO pageReqVO) {
        return processorsMapper.selectPage(pageReqVO);
    }

    @Override
    @Slave
    @TenantIgnore
    public List<ProcessorsDO> getProcessorList(Integer deviceId) {
        LambdaQueryWrapperX<ProcessorsDO> processorsWrapper = new LambdaQueryWrapperX<ProcessorsDO>();
        processorsWrapper.eq(ProcessorsDO::getDeviceId, deviceId);
        return processorsMapper.selectList(processorsWrapper);
    }

    @Override
    @Slave
    @TenantIgnore
    public Double getCpuUsage(Integer deviceId) {
        LambdaQueryWrapperX<ProcessorsDO> processorsWrapper = new LambdaQueryWrapperX<ProcessorsDO>();
        processorsWrapper.eq(ProcessorsDO::getDeviceId, deviceId);
        List<ProcessorsDO> processorsDOS = processorsMapper.selectList(processorsWrapper);

        if (processorsDOS == null || processorsDOS.isEmpty()) {
            // 防止除以0
            return 0.0;
        }

        double average = processorsDOS.stream()
                .mapToInt(ProcessorsDO::getProcessorUsage)  // 假设存在 getProcessorUsage() 方法
                .average()
                .orElse(0.0);  // 如果为空则返回 0.0

        // 保留两位小数
        return Math.round(average * 100.0) / 100.0;

    }

}